package com.smartaccounting.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateUtil {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter MONTH_YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");

    public static String formatDate(LocalDate date) {
        return date.format(DATE_FORMATTER);
    }

    public static LocalDate parseDate(String dateString) {
        return LocalDate.parse(dateString, DATE_FORMATTER);
    }

    public static String formatMonthYear(LocalDate date) {
        return date.format(MONTH_YEAR_FORMATTER);
    }

    public static String getCurrentMonthYear() {
        return formatMonthYear(LocalDate.now());
    }

    public static LocalDate getFirstDayOfMonth(String monthYear) {
        return LocalDate.parse(monthYear + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static LocalDate getLastDayOfMonth(String monthYear) {
        LocalDate firstDay = getFirstDayOfMonth(monthYear);
        return firstDay.withDayOfMonth(firstDay.lengthOfMonth());
    }
}